<?php
namespace App\Admin\Forms;

use App\Models\Shop\WxShopClassify;
use App\Models\Shop\WxShopGoods;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;

class ShopGoodsTransferForm extends Form implements LazyRenderable
{
    use LazyWidget;
    public function handle(array $input)
    {

        //接收弹窗提交过来的数据，进行处理

        $ids = explode(',', ($input['id'] ?? '')); //处理提交过来的批量选择的行的id

        if (!$ids) {
            return $this->response()->error('参数错误');
        }

        $classify_id = $input['classify_id'] ?? 0;
        if($classify_id > 0){
            $rows = WxShopGoods::whereIn('id', $ids)->update(['classify_id'=>(int)$classify_id]);
        }else{
            return $this->response()->error('参数错误');
        }

        return $this->response()->success('提交成功')->refresh();


    }

    /**
     * Build a form here.
     */
    public function form()
    {
        //弹窗界面
        $this->radio('classify_id', '商品分类id')->options(WxShopClassify::pluck( 'name','id'));
        //批量选择的行的值怎么传递看下面
        $this->hidden('id')->attribute('id', 'transfer-goods-ids'); //批量选择的行的id通过隐藏元素 提交时一并传递过去
    }

    /**
     * The data of the form.
     *
     * @return array
     */
    public function default()
    {
        //设置默认值
        return [
        ];
    }
}
